#include "kernel_operator.h"
#include <type_traits>
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;

//case5 half 无广播
template<typename TYPE_Y> class KernelPows {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelPows() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;
        
        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X1)+32);
        pipe.InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X2)+32);
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y)+32);
        if constexpr (! std::is_same_v<T, float>) 
        {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(float));
        }

    }
    __aicore__ inline void Process() {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
        this->processDataNum = this->tailDataNum;
        CopyIn(loopCount);
        Compute(loopCount);
        CopyOut(loopCount);
        
    }
    private:
    __aicore__ inline void CopyIn(int32_t progress) {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.AllocTensor<DTYPE_X1>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);

        LocalTensor<DTYPE_X2> x2Local = inQueueX2.AllocTensor<DTYPE_X2>();
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(int32_t progress) {
        
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.DeQue<DTYPE_X1>();
        
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();

        if constexpr (std::is_same_v<T, float>) {

            Ln(yLocal, x1Local, this->processDataNum);
            inQueueX1.FreeTensor(x1Local);
            LocalTensor<DTYPE_X2> x2Local = inQueueX2.DeQue<DTYPE_X2>();
            Mul(yLocal, yLocal, x2Local, this->processDataNum);
            Exp(yLocal, yLocal, this->processDataNum);

            inQueueX2.FreeTensor(x2Local);

        }
        else{
            
            auto tmp1 = QueueTmp1.Get<float>();
            auto tmp2 = QueueTmp2.Get<float>();
            LocalTensor<DTYPE_X2> x2Local = inQueueX2.DeQue<DTYPE_X2>();

            Cast(tmp1, x1Local, RoundMode::CAST_NONE, this->processDataNum);
            Ln(tmp1, tmp1, this->processDataNum);
            Cast(tmp2, x2Local, RoundMode::CAST_NONE, this->processDataNum);
            Mul(tmp1, tmp1, tmp2, this->processDataNum);
            Exp(tmp1,tmp1, this->processDataNum);

            Cast(yLocal, tmp1, RoundMode::CAST_RINT, this->processDataNum);

            inQueueX1.FreeTensor(x1Local);
            inQueueX2.FreeTensor(x2Local);
        }        
        outQueueY.EnQue<TYPE_Y>(yLocal);
        
        
    }
    __aicore__ inline void CopyOut(int32_t progress) {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmpbit, QueueTmp1, QueueTmp2,boox;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

template<typename TYPE_Y> class KernelPows_broadcast {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelPows_broadcast () {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                            uint32_t size,uint32_t y_dim, uint32_t* x1_shape, uint32_t* x2_shape, uint32_t* y_shape) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->size = size;
        this->y_dim = y_dim;
        for(uint32_t i=0; i<this->y_dim; i++)
        {
            this->x1_shape[i] = x1_shape[i];
            this->x2_shape[i] = x2_shape[i];
            this->y_shape[i] = y_shape[i];
        }
        
        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, this->size);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, this->size);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->size);

        pipe.InitBuffer(QueueTmp1, 32 * sizeof(float));
        pipe.InitBuffer(QueueTmp2, 32 * sizeof(float));
        pipe.InitBuffer(QueueTmp3, 32 * sizeof(float));
    }
    __aicore__ inline void Process() {
        for(int32_t i=0; i<this->size; i++)
        {
            if constexpr (std::is_same_v<T, float>) {
                auto tmp1 = QueueTmp1.Get<float>();
                auto tmp2 = QueueTmp2.Get<float>();

                tmp1.SetValue(0, x1Gm.GetValue(get_broadcasted_index(i, x1_shape, y_shape, y_dim)));
                tmp2.SetValue(0, x2Gm.GetValue(get_broadcasted_index(i, x2_shape, y_shape, y_dim)));

                if(tmp1.GetValue(0) == 0)
                {
                }
                else
                {
                    Ln(tmp1, tmp1, 1);
                    Mul(tmp1, tmp1, tmp2, 1);
                    Exp(tmp1,tmp1,1);
                }

                yGm.SetValue(i, tmp1.GetValue(0));
            }
            else if constexpr (std::is_same_v<T, half>) {
                auto tmp1 = QueueTmp1.Get<float>();
                auto tmp2 = QueueTmp2.Get<float>();

                auto tmp3 = QueueTmp3.Get<half>();

                tmp3.SetValue(0, x1Gm.GetValue(get_broadcasted_index(i, x1_shape, y_shape, y_dim)));
                Cast(tmp1, tmp3, RoundMode::CAST_NONE, 1);

                tmp3.SetValue(0, x2Gm.GetValue(get_broadcasted_index(i, x2_shape, y_shape, y_dim)));
                Cast(tmp2, tmp3, RoundMode::CAST_NONE, 1);

                if(tmp1.GetValue(0) == 0)
                {
                }
                else
                {
                    Ln(tmp1, tmp1, 1);
                    Mul(tmp1, tmp1, tmp2, 1);
                    Exp(tmp1,tmp1,1);
                }

                Cast(tmp3, tmp1, RoundMode::CAST_NONE, 1);
                yGm.SetValue(i, tmp3.GetValue(0));
            }
            else if constexpr (std::is_same_v<T, bfloat16_t>) {
                auto tmp1 = QueueTmp1.Get<float>();
                auto tmp2 = QueueTmp2.Get<float>();

                auto tmp3 = QueueTmp3.Get<bfloat16_t>();

                tmp3.SetValue(0, x1Gm.GetValue(get_broadcasted_index(i, x1_shape, y_shape, y_dim)));
                Cast(tmp1, tmp3, RoundMode::CAST_NONE, 1);

                tmp3.SetValue(0, x2Gm.GetValue(get_broadcasted_index(i, x2_shape, y_shape, y_dim)));
                Cast(tmp2, tmp3, RoundMode::CAST_NONE, 1);

                if(tmp1.GetValue(0) == 0)
                {
                }
                else
                {
                    Ln(tmp1, tmp1, 1);
                    Mul(tmp1, tmp1, tmp2, 1);
                    Exp(tmp1,tmp1,1);
                }

                Cast(tmp3, tmp1, RoundMode::CAST_RINT, 1);
                yGm.SetValue(i, tmp3.GetValue(0));
            }
        }
    }
private:
__aicore__ inline uint32_t get_broadcasted_index(uint32_t indices, uint32_t *original_shape, uint32_t *broadcast_shape, uint32_t ndim)
{
    uint32_t shape[8];
    uint32_t original_indices = 0;
    for(int32_t i=ndim-1; i>=0; i--)
    {
        shape[i] = 0;
        if(original_shape[i]!=1)
        {
            shape[i] = indices % broadcast_shape[i];
        }
        indices /= broadcast_shape[i];        
    }
    for(uint32_t i=0; i<ndim; i++)
    {
        original_indices = shape[i] + original_indices * original_shape[i];
    }
    return original_indices;
}
private:
    TPipe pipe;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2, QueueTmp3;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t size;

    uint32_t y_dim;
    uint32_t x1_shape[8];
    uint32_t x2_shape[8];
    uint32_t y_shape[8];
};

extern "C" __global__ __aicore__ void pows(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    if (TILING_KEY_IS(1)) {
        KernelPows<DTYPE_X1> op;
        op.Init(x1, x2, y, 
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);
        op.Process();
    }
    else if (TILING_KEY_IS(2)) {
        KernelPows_broadcast<DTYPE_X1> op;
        op.Init(x1, x2, y, 
                tiling_data.size, tiling_data.y_dim, tiling_data.x1_shape, tiling_data.x2_shape, tiling_data.y_shape);
        op.Process();
    }
}